package frame.project;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

/**
 * @author Jourdan
 *
 */

public class DBAdapter {
	//Database properties
	static final String TAG = "DBAdapter";
	static final String DATABASE_NAME = "framedb";
	static final int DATABASE_VERSION = 1;
	
	//Table name
	static final String TABLE_PATH = "path";
	static final String TABLE_LABEL = "label";
	
	//Attribute for TABLE_PATH
	static final String PATH_ID = "_pathid";
	static final String PATH_STRING = "pathstring";
	static final String PATH_DATE_TIME = "pathdatetime";
	static final String PATH_COLOR = "pathcolor";	
	static final String PATH_DESCRIPTION = "pathdescription";
	
	//Attribute for TABLE_LABEL
	static final String LABEL_ID = "_labelid";
	static final String LABEL_NAME = "labelname";
	static final String LABEL_COLOR = "labelcolor";
	
	//Create table SQL
	static final String CREATE_TABLE_PATH = "create table "+ TABLE_PATH + " (" +
			PATH_ID + " integer primary key autoincrement, " +
			PATH_STRING + " text not null, " +
			PATH_DATE_TIME + " text not null, " +
			PATH_COLOR + " text not null," +
			PATH_DESCRIPTION + " text);";
	static final String CREATE_TABLE_LABEL = "create table "+ TABLE_LABEL +" (" +
			LABEL_ID + " integer primary key autoincrement, "+
			LABEL_NAME + " text not null, " +
			LABEL_COLOR + " text not null);";
	
	final Context context;
	
	DatabaseHelper DBHelper;
	SQLiteDatabase db;
	
	public DBAdapter(Context ctx) {
		this.context = ctx;
		DBHelper = new DatabaseHelper(context);
	}
	
	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		
		@Override
		public void onCreate(SQLiteDatabase db) {
			try {
				//Create PATH table
				db.execSQL(CREATE_TABLE_PATH);
				//Create and insert LABEL table
				db.execSQL(CREATE_TABLE_LABEL);
				ContentValues labelNameValues = new ContentValues();
				//labelNameValues.put(LABEL_NAME, "Untitled");
				String labelColorArray[] = {"Red", "Orange", "Yellow", "Green", "Blue", "Pink", "Purple"};
				
				for(int i = 0; i < labelColorArray.length; i++) {
					labelNameValues.put(LABEL_NAME, "Untitled");
					labelNameValues.put(LABEL_COLOR, labelColorArray[i]);
					db.insert(TABLE_LABEL, null, labelNameValues);
					//insertLabel("Untitled",labelColorArray[i]);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
			db.execSQL("DROP TABLE IF EXISTS PATH");
			onCreate(db);
		}
	}
	
	//---opens the database---
	public DBAdapter open() throws SQLException {
		db = DBHelper.getWritableDatabase();
		return this;
	}
	
	//---closes the database---
	public void close() {
		DBHelper.close();
	}
	
	/**
	 * This function is used to insert path into TABLE_PATH
	 * @param path
	 * @param dateTime
	 * @param color
	 * @param description
	 * @return
	 */
	public long insertPath(String path, String dateTime, String color, String description) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(PATH_STRING, path);
		initialValues.put(PATH_DATE_TIME, dateTime);
		initialValues.put(PATH_COLOR, color);
		initialValues.put(PATH_DESCRIPTION, description);
		return db.insert(TABLE_PATH, null, initialValues);
	}
	
	/**
	 * This function is used to insert label into TABLE_LABEL
	 * @param name
	 * @param color
	 * @return
	 */
	public long insertLabel(String name, String color) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(LABEL_NAME, name);
		initialValues.put(LABEL_COLOR, color);
		return db.insert(TABLE_PATH, null, initialValues);
	}
	
	//---retrieves all the path's string, path's dateTime, path's color, and path's description---
	public Cursor getPath() {
		return db.query(TABLE_PATH, new String[] {PATH_ID, PATH_STRING, PATH_DATE_TIME, PATH_COLOR, PATH_DESCRIPTION}, null, null, null, null, PATH_DATE_TIME);
	}
	
	public Cursor getLabel() throws SQLException {
		Cursor mCursor = db.query(true, TABLE_LABEL, new String[] {LABEL_NAME, LABEL_COLOR}, null, null, null, null, null, null);
		if (mCursor != null){
			mCursor.moveToFirst();
		}
		
		return mCursor;
	}
	
	/**
	 * This function is used to update path into TABLE_PATH
	 * @param rowId
	 * @param path
	 * @param dateTime
	 * @param color
	 * @param description
	 * @return
	 */
	public boolean updatePath(long rowId, String path, String dateTime, String color, String description) {
		ContentValues updateValues = new ContentValues();
		updateValues.put(PATH_STRING, path);
		updateValues.put(PATH_DATE_TIME, dateTime);
		updateValues.put(PATH_COLOR, color);
		updateValues.put(PATH_DESCRIPTION, description);
		return db.update(TABLE_PATH, updateValues, PATH_ID + "=" + rowId, null) > 0;
	}
	
	/**
	 * This function is used to update path into TABLE_LABEL
	 * @param rowId
	 * @param name
	 * @param color
	 * @return
	 */
	public boolean updateLabel(long rowId, String name, String color) {
		ContentValues updateValues = new ContentValues();
		updateValues.put(LABEL_NAME, name);
		updateValues.put(LABEL_COLOR, color);
		return db.update(TABLE_LABEL, updateValues, LABEL_ID + "=" + rowId, null) > 0;
	}
}
